package client.view;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

import net.GameInfo;
import client.AccountProxy;
import client.Client;
import client.LobbyClient;

/**
 * A visualization of the MasterServer. Users use this interface to join tables
 * (GameServers) and to chat with other players.
 * The MasterServerView is an Observer registered to view the MasterServer.
 */
public class MasterServerView implements PropertyChangeListener {
	/** The JFrame that displays the lobby */
	private JFrame frame;
	private TableList tables;
	private AccountList online;
	private ChatPane cp;
	private LobbyClient client;
	private PrivateChatFrame pcf;
	private AccountProxy me;
	/**
	 * Create a new MasterServerView to represent the remote MasterServer.
	 * LobbyClients will use the LobbyView to communicate with the MasterServer
	 * and with other players logged on through the MasterServer.
	 */

	public MasterServerView(LobbyClient client, AccountProxy me) {
		this.me = me;
		System.out.println("msv me is " + me);
		this.client = client;
		createInterface();
		frame.setVisible(true);
		pcf = new PrivateChatFrame(client);
	}

	/**
	 * Creates the components involved in the interface of the LobbyView
	 */
	private void createInterface() {
		frame = new JFrame("GCCF Lobby");
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
				try {
					client.sendGuiClosed(me.getUserName());
					System.exit(0);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
		frame.setSize(800, 600);
		JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
		jsp.setDividerLocation(frame.getWidth()/2);
		JPanel tablesPanel = new JPanel(new BorderLayout());
		jsp.setLeftComponent(tablesPanel);
		tables=new TableList(client);
		JButton jb = new JButton("Join Selected Game");
		jb.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent me) {
				if (tables.getSelectedValue() != null) {
					final TableView tv = (TableView) (tables.getSelectedValue());
					new Thread() {
						public void run() {
							try {
								new Client(tv.getGameInfo()).run();
							} catch (IOException e) {e.printStackTrace();}
						}
					}.start();
				}
				else
					JOptionPane.showMessageDialog(frame,"Please select a table first");
			}
		});
		tablesPanel.add(jb,BorderLayout.NORTH);
		JSplitPane rightSP = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
		rightSP.setDividerLocation(frame.getWidth()/4);
		cp = new ChatPane(new LobbyChatAction(this));
		JPanel chatPanel = new JPanel(new BorderLayout());
		chatPanel.add(cp);
		rightSP.setLeftComponent(chatPanel);
		rightSP.setRightComponent(new AccountList(client,me));
		jsp.setRightComponent(rightSP);
		
		online=new AccountList(client,me);
		JButton pm = new JButton("Send Private Message to Selected User");
		pm.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent me) {
				if (online.getSelectedValue() != null) {
					AccountProxyView apv = (AccountProxyView)(online.getSelectedValue());
					pcf.showTab(apv.getAccountProxy());
				} else {
					JOptionPane.showMessageDialog(frame,"Please select a user first");
				}
			}
		});
		JPanel pmPanel = new JPanel(new BorderLayout());
		pmPanel.add(pm,BorderLayout.NORTH);
		pmPanel.add(online);
		rightSP.setRightComponent(pmPanel);
		tablesPanel.add(tables);
		frame.add(jsp);
		frame.setVisible(true);
	}
	public ChatPane getChatPane() { return cp; }
	public LobbyClient getClient() { return client; }
	public static void main(String args[]) {
		new MasterServerView(null,null);
	}

	public void propertyChange(PropertyChangeEvent arg0) {
		String cmd=arg0.getPropertyName();
		if (cmd.equals("addTable")) {
			tables.addTable((GameInfo)(arg0.getNewValue()));
		} else if (cmd.equals("removeTable")) {
			tables.removeTable((GameInfo)(arg0.getOldValue()));
		} else if (cmd.equals("addClient")) {
			System.out.println("add client to list");
			online.addAccount((AccountProxy)(arg0.getNewValue()));
		} else if (cmd.equals("removeClient")) {
			online.removeAccount((AccountProxy)(arg0.getOldValue()));
		} else if (cmd.equals("lobbyMessage")) {
			cp.appendMessage((String)(arg0.getNewValue()));
		} else if (cmd.equals("privateMessage")) {
			pcf.readMessage((String)(arg0.getNewValue()));
		}
	}
}
